package dome3;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DickYang
 * Date: 2023-05-09
 * Time: 17:43
 */
//姓名比较器
class NameComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        if (o1.name.compareTo(o2.name) > 0) {
            return 1;
        }else if (o1.name.compareTo(o2.name) < 0) {
            return -1;
        }else {
            return 0;
        }
    }
}
//年龄比较器
class AgeComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}

public class Test {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",12,34);
        students[1] = new Student("abc",45,78);
        students[2] = new Student("lisi",34,234);

        AgeComparator ageComparator = new AgeComparator();
        NameComparator nameComparator = new NameComparator();

        //调用sort的方法,第一个参数传数组名,第二个参数传比价器.
        //使用比较器可以使代码变得更灵活!
        Arrays.sort(students,nameComparator);

        System.out.println(Arrays.toString(students));

    }
}
